package aps.module_Machines;

//Java
import org.lwjgl.opengl.GL11;

//Minecraft
import net.minecraft.src.GuiContainer;
import net.minecraft.src.InventoryPlayer;
import net.minecraft.src.Block;

//Forge
import net.minecraftforge.client.MinecraftForgeClient;

//APS
import aps.module_Core.ContainerAPS;
import aps.BuildcraftAPS;



public class GuiExtractor extends GuiContainer
{
	private TileEntityExtractor extractorInventory;
	String guiFile = BuildcraftAPS.imageFilesRoot + "ExtractorGUI.png";
	
	int guiTex;
	
	public GuiExtractor(TileEntityExtractor extractor, InventoryPlayer playerInv)
	{
		super(new ContainerExtractor(extractor, playerInv));
        extractorInventory = extractor;
	}
	
	protected void drawGuiContainerForegroundLayer(int x, int y)
    {
		fontRenderer.drawString("Traces Detected:", 42, 19, 0x404040);
        int i = 0;
        for(String S : extractorInventory.getPossibleProducts())
        {
        	fontRenderer.drawString(S, 42, 29 + (10 * i), 0x404040);
        	i++;
        }
    }
	
	protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
    {
        guiTex = mc.renderEngine.getTexture(guiFile);
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        mc.renderEngine.bindTexture(guiTex);
        int topLeftX = (width - xSize) / 2;
        int topLeftY = (height - ySize) / 2;
        drawTexturedModalRect(topLeftX, topLeftY, 0, 0, xSize, ySize);
        displayTempGauge(topLeftX, topLeftY, 8, 16, (int) extractorInventory.getScaledPowerLevel(64));
        displayLiquidGauge(topLeftX, topLeftY, 22, 19, (int) extractorInventory.getScaledLiquidQuantity(58), extractorInventory.LavaID);
    }
	
	private void displayTempGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight)
	{
		int start = 0;
		int texPosX;
		if(extractorInventory.getPowerSufficient())
			texPosX = 184;
		else
			texPosX = 176;
		
		while (true)
		{
			int x = 0;

			if (BarHeight > 8) {
				x = 8;
				BarHeight -= 8;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 64 - x - start, texPosX, 8 - x, 8, x); //The 58 is the gauge height
			start = start + 8;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
    }
	
	private void displayLiquidGauge(int TopLeftX, int TopLeftY, int GaugeTopLeftX, int GaugeTopLeftY, int BarHeight, int LiquidId)
	{
		Object o = Block.blocksList[LiquidId];
		int liquidImgIndex = Block.blocksList[LiquidId].blockIndexInTexture;
		
		if (o == null) {
			return;
		}

		mc.renderEngine.bindTexture(mc.renderEngine.getTexture("/terrain.png"));

		int imgLine = liquidImgIndex / 16;
		int imgColumn = liquidImgIndex - imgLine * 16;

		int start = 0;

		while (true)
		{
			int x = 0;

			if (BarHeight > 16) {
				x = 16;
				BarHeight -= 16;
			} else {
				x = BarHeight;
				BarHeight = 0;
			}

			drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY + 58 - x - start, imgColumn * 16, (imgLine * 16) + (16 - x), 16, x); //The 58 is the gauge height
			start = start + 16;

			if (x == 0 || BarHeight == 0) {
				break;
			}
		} //This while draws the repeating gauge graphic.
		mc.renderEngine.bindTexture(guiTex);
		drawTexturedModalRect(TopLeftX + GaugeTopLeftX, TopLeftY + GaugeTopLeftY, 176, 8, 16, 60); //This draws the gauge measurement lines on top
    }
}